package com.huangmianwei.modules.system.api;

import com.huangmianwei.modules.common.dto.output.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.util.UUID;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author dawn
 * Date on 2021/1/7  15:11
 */
@RestController
@RequestMapping("/api/uplaod/images")
public class UploadController {

  @PostMapping(value = "/saveImage")
  public ApiResult upFile(@RequestParam(value = "file",required=false) MultipartFile file) {
    String path = "D:/下载/imgs/";
    String fileName = file.getOriginalFilename();
    String suffixName=fileName.substring(fileName.lastIndexOf("."));
    fileName= UUID.randomUUID()+suffixName;
    System.out.println(fileName);
    System.out.println("type::" + suffixName);
    System.out.println("filename::" + fileName);
    File targetFile = new File(path);
    if (!targetFile.exists()) {
      targetFile.mkdirs();
    }
    File saveFile = new File(targetFile, fileName);
    try {
      file.transferTo(saveFile);
      System.out.println("执行成功");
      String path1 = path + fileName;
      System.out.println(path1);
      System.out.println("11111");
      return ApiResult.ok("上传成功", path1);

    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("执行失败");
      return ApiResult.ok("上传失败");
    }
    //     return "success";

  }

}
